/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.calendar.eventeditor;

import java.util.TimeZone;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import mtt.calendar.eventeditor.events.EditEventEvent;
import mtt.calendar.weekview.events.ShowWeekViewEvent;
import mtt.rms.dataObjects.Event;
import mtt.rms.recordstores.calendar.events.AddEventToRmsEvent;
import mtt.rms.recordstores.calendar.events.DeleteEventFromRmsEvent;

import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.TerminateEvent;
import hmvc.j2me.view.AView;

/**
 *
 *
 * @author jens.meiss
 */
public class EventEditorView extends AView {
	
	private static final Form form 	= new Form("MTT - Termindetails");
	
	private static TextField 	textFieldName;
	private static TextField 	textFieldOrt;
	private static ChoiceGroup 	choiceGroupTyp;
	private static DateField 	dateFieldBeginDateOfEvent;
	private static DateField	dateFieldTimeOfBegin;
	private static DateField	dateFieldTimeOfEnd;
	private static DateField 	dateFieldEndDateOfEvent;
	private static TextField 	textFieldIntervall;
	private static TextField 	textFieldTutor;
	private static TextField	textFieldInfoText;
	
	private Command commandBack = new Command("zurück", Command.BACK, 0);
	private Command commandStore = new Command("Speichern", "Änderungen Speichern", Command.SCREEN, 1);
	private Command commandDelete = new Command("Löschen", "Termin löschen", Command.SCREEN, 1);
	
	/**
	 * contains the currently edited Event
	 */
	private Event event;

	public void handleEvent(IEvent event) {
		
		if (event instanceof EditEventEvent) {
			EditEventEvent editEvent = (EditEventEvent) event;
			initEvent(editEvent.getEvent());
		}
		
	}

	private void initEvent(Event event) {
		
		if (event == null) {
			event = new Event();
		} 
		
		setEvent(event);
		
		/*
		 * cleanup the form
		 */
		form.removeCommand(commandBack);
		form.removeCommand(commandDelete);
		form.removeCommand(commandStore);
		form.deleteAll();
		
		/*
		 * Textfelder füllen
		 */
		textFieldName	= new TextField("Name: ", 	event.getName(), 25, TextField.ANY);
		textFieldOrt	= new TextField("Ort: ", 	event.getPlace(), 25, TextField.ANY);
		textFieldTutor 	= new TextField("Dozent: ", event.getTutor(), 25, TextField.ANY);
		
		/*
		 * ChoiceGroup Typ erstellen
		 */
		choiceGroupTyp	= new ChoiceGroup("Typ: ", ChoiceGroup.POPUP);
		choiceGroupTyp.append("VO - Vorlesung Obligatorisch", null);
		choiceGroupTyp.append("VW - Vorlesung Wahlpflicht", null);
		choiceGroupTyp.append("PO - Praktikum Obligatorisch", null);
		choiceGroupTyp.append("PW - Praktikum - Wahlpflicht", null);
		choiceGroupTyp.append("ÜO - Übung Obligatorisch", null);
		choiceGroupTyp.append("LT - Lerngruppen-Event", null);
		choiceGroupTyp.append("Undefinierter Termin", null);
		choiceGroupTyp.setSelectedIndex(event.getType(), true);
		
		dateFieldBeginDateOfEvent 	= new DateField("Tag begin:", DateField.DATE, TimeZone.getDefault());
		dateFieldTimeOfBegin		= new DateField("Uhzeit begin:", DateField.TIME, TimeZone.getDefault());
		dateFieldEndDateOfEvent 	= new DateField("Tag ende:", DateField.DATE, TimeZone.getDefault());
		dateFieldTimeOfEnd			= new DateField("Uhzeit ende:", DateField.TIME, TimeZone.getDefault());
		
		/*
		 * ChoiceGroup Intervall
		 */
		textFieldIntervall = new TextField("Intervall in Wochen: ", "0" + event.getIntervall(), 2, TextField.DECIMAL);
		
		textFieldInfoText = new TextField("zusatz Informationen", event.getInfoText(), 255, TextField.ANY);
		
		form.append(textFieldName);
		form.append(textFieldOrt);
		form.append(textFieldTutor);
		form.append(choiceGroupTyp);
		form.append(dateFieldBeginDateOfEvent);
		form.append(dateFieldTimeOfBegin);
		form.append(dateFieldEndDateOfEvent);
		form.append(dateFieldTimeOfEnd);
		form.append(textFieldIntervall);
		form.append(textFieldInfoText);
		
		form.addCommand(commandBack);
		form.addCommand(commandDelete);
		form.addCommand(commandStore);
		
		
	}

	public void terminate() {
		// TODO Auto-generated method stub
		
	}

	public void init() {
		
		form.addCommand(commandBack);
		form.addCommand(commandStore);
		form.addCommand(commandDelete);
		
		setDisplayAble(form);
		
	}

	public void registerEvents() {
		getController().register(EditEventEvent.class, this);
	}

	public void commandAction(Command c, Displayable d) {
		
		if (c == commandBack) {
			getController().fireEventUp(new ShowWeekViewEvent());
			getController().fireEventLocal(new TerminateEvent());
		} else if (c == commandStore) {
			getController().fireEventUp(new AddEventToRmsEvent(getEvent()));
		} else if (c == commandDelete) {
			
			if (getEvent().getRecordId() > 0) {
				getController().fireEventUp((new DeleteEventFromRmsEvent(getEvent())));
			}
			
			getController().fireEventUp(new ShowWeekViewEvent());
			getController().fireEventLocal(new TerminateEvent());
		}
		
	}

	private Event getEvent() {
		
		event.setDateEndOfEvent(dateFieldEndDateOfEvent.getDate());
		event.setDateStartOfEvent(dateFieldBeginDateOfEvent.getDate());
		event.setInfoText(textFieldInfoText.getString());
		
		event.setIntervall(Byte.parseByte(textFieldIntervall.getString()));
		event.setName(textFieldName.getString());
		event.setPlace(textFieldOrt.getString());
		event.setTimeBeginOfEvent((int) dateFieldTimeOfBegin.getDate().getTime());
		event.setTimeEndOfEvent((int) dateFieldTimeOfEnd.getDate().getTime());
		event.setTutor(textFieldTutor.getString());
		//TODO: typ nicht direkt über choiceGroupTyp setzen
		event.setType(choiceGroupTyp.getSelectedIndex());
		
		
		return event;
	}

	private void setEvent(Event event) {
		this.event = event;
	}

}
